﻿Imports TNCDAL
Imports System.Linq
Imports System.Drawing

Public Class ucMBL_Voyage_FreightTariff
    Dim db As New TanNamChinhDataContext
    Dim dtBangCuocBuying As New DataTable
    Dim dtBangCuocSelling As New DataTable

    Private m_MBL As New MBLRepository
    Private m_Voyage As New CVoyage
    Private obj_MBL As tblMBL
    Private obj_Voyage As tblVoyage
    Private m_ID_File As Integer

    Public Sub New(ByVal ID_File As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_ID_File = ID_File
    End Sub

    Private Sub ucMBL_Voyage_FreightTariff_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Kiểm tra file đã nhập loại tiền chưa
        Dim obj_File As tblFile
        Dim m_File As New FileRepository
        obj_File = m_File.SelectRecordById(m_ID_File)

        'If obj_File.ID_Currency Is Nothing Then
        '    MsgBox("Bạn chưa chọn loại tiền tệ để tạo bảng cước tàu")
        '    btnGhi.Enabled = False
        '    btnSua.Enabled = False
        '    btnXoa.Enabled = False
        '    btnHuy.Enabled = False
        '    Exit Sub
        'End If

        'Kiểm tra đã tạo HBL và Cont chưa

        KhoiTaolvContType()
        KhoiTaocbbCurrency()
        KhoiTaocbbCurrencySelling()
        KhoiTaoGiaoDien(True)

        KhoiTaoMBL_Voyage_FreightTariff(m_ID_File)
        'ExpandablePanelList.Width = PanelEx2.Width / 2
        'ExpnlBuying.Height = (Me.Size.Height - PanelEx2.Height - pnlButton.Height) / 2

        AddComboBoxColumns()
    End Sub

    Public Sub KhoiTaoThuocTinhlvContType()
        lvContType.View = View.Details
        lvContType.BackColor = Color.White
        lvContType.FullRowSelect = True
        lvContType.Columns.Add("Loại Cont", 95)
    End Sub

    Public Sub KhoiTaolvContType()
        KhoiTaoThuocTinhlvContType()

        Dim contType = From p In db.tblContTypes
                       Where p.IsActive = True
                        Select p Order By p.ContTypeName Ascending

        lvContType.Items.Clear()

        For Each p As tblContType In contType
            Dim item As New ListViewItem
            item.Text = p.ContTypeName
            item.Name = p.ID_ContType
            lvContType.Items.Add(item)
        Next
    End Sub

    Public Sub KhoiTaoMBL_Voyage_FreightTariff(ByVal ID_File As Integer)
        Dim m_File As New FileRepository
        Dim File = m_File.SelectRecordById(ID_File)

        '1. MBL
        'Chọn MBL đầu tiên của File
        Dim c_MBL = (From p In db.tblFileMBLs
                  Where p.ID_File = ID_File
                  Select p).Count

        If c_MBL > 0 Then
            Dim FileMBL = (From p In db.tblFileMBLs
                  Where p.ID_File = ID_File
                  Select p).Single

            obj_MBL = m_MBL.SelectRecordById(FileMBL.ID_MBL)
            'txtMBLNo.Text = obj_MBL.MBLNo
            'Kiem tra da tao Voyage chua?
            If obj_MBL.ID_Voyage Is Nothing Then
                'ClearVoyage()
                MsgBox("Chưa tạo Voyage")
                Exit Sub
            End If
        Else
            ' Chưa có MBL
            ' ClearVoyage and MBL
            MsgBox("Chưa tạo MBL")
            Exit Sub
        End If

        '2. Voyage
        obj_Voyage = m_Voyage.SelectRecordById(obj_MBL.ID_Voyage)


        '3 ContType: Insert ContType in HBL and not in FreightTariff

        'a. ContType_HBL
        'Lay danh sach HBL theo MBL
        Dim listHBL = From p In db.tblHBLs
                              Where p.tblFileMBL.ID_File = ID_File
                              Select p

        Dim list_ContType_HBL As New ArrayList
        'Lay danh sach conttype da nhap trong cac HBL
        For Each HBL As tblHBL In listHBL
            Dim ID_HBL As Integer = HBL.ID_HBL
            Dim listCont = (From p In db.tblConts
                                Where p.ID_HBL = ID_HBL
                                Select p.ID_ContType).Distinct.ToList

            'kiem tra trung loai conttype

            For j As Integer = 0 To listCont.Count - 1
                Dim flag_KoTrung As Boolean = True

                For i As Integer = 0 To list_ContType_HBL.Count - 1
                    If list_ContType_HBL.Item(i) = listCont.Item(j) Then
                        flag_KoTrung = False
                    End If
                Next

                If flag_KoTrung Then
                    list_ContType_HBL.Add(listCont.Item(j))
                End If
            Next
        Next

        'b. ContType in FreightTariff

        Dim list_ContType_FT = (From p In db.tblFreightTariffs
                              Where p.ID_Voyage = obj_Voyage.ID_Voyage
                              Select p.ID_ContType).Distinct.ToList

        'c Insert ContType in HBL and not in FreightTariff
        For Each contType In list_ContType_HBL
            Dim flag_NotExist As Boolean = False
            For i As Integer = 0 To list_ContType_FT.Count - 1
                If contType = list_ContType_FT(i) Then
                    flag_NotExist = True
                    Exit For
                End If
            Next

            If Not flag_NotExist Then
                'Insert contType in FreightTariff
                'a. Insert Buying
                Dim listFee_Buying = From p In db.tblFees
                    Where (p.ID_FeeType = 1 And p.IsBill = False And p.IsActive = True)
                    Select p

                For Each fee In listFee_Buying
                    Dim obj_FT As New tblFreightTariff
                    obj_FT.ID_Voyage = obj_Voyage.ID_Voyage
                    obj_FT.ID_Fee = fee.ID_Fee
                    obj_FT.ID_ContType = contType
                    obj_FT.Price = 0
                    obj_FT.ID_Currency = 2 'File.ID_Currency
                    obj_FT.IsBuying = True

                    db.tblFreightTariffs.InsertOnSubmit(obj_FT)
                    db.SubmitChanges()
                Next
                'b. Insert Selling
                Dim listFee_Selling = From p In db.tblFees
                    Where (p.ID_FeeType = 2 And p.IsBill = False And p.IsActive = True)
                    Select p

                For Each fee In listFee_Selling
                    Dim obj_FT As New tblFreightTariff
                    obj_FT.ID_Voyage = obj_Voyage.ID_Voyage
                    obj_FT.ID_Fee = fee.ID_Fee
                    obj_FT.ID_ContType = contType
                    obj_FT.Price = 0
                    obj_FT.ID_Currency = 2 'File.ID_Currency
                    obj_FT.IsBuying = False

                    db.tblFreightTariffs.InsertOnSubmit(obj_FT)
                    db.SubmitChanges()
                Next
            End If
           
        Next



        ''3. FreightTariff
        ''Kiem tra da co Freight Tarrif chưa? Nếu chưa thì tạo
        'Dim c_FreightTariff = (From p In db.tblFreightTariffs
        '                      Where p.ID_Voyage = obj_Voyage.ID_Voyage
        '                      Select p).Count

        'If c_FreightTariff = 0 Then
        '    'Lay danh sach HBL theo MBL
        '    Dim listHBL = From p In db.tblHBLs
        '                          Where p.tblFileMBL.ID_File = ID_File
        '                          Select p

        '    Dim list_ContType As New ArrayList
        '    'Lay danh sach conttype da nhap trong cac HBL
        '    For Each HBL As tblHBL In listHBL
        '        Dim ID_HBL As Integer = HBL.ID_HBL
        '        Dim listCont = (From p In db.tblConts
        '                            Where p.ID_HBL = ID_HBL
        '                            Select p.ID_ContType).Distinct.ToList

        '        'kiem tra trung loai conttype

        '        For j As Integer = 0 To listCont.Count - 1
        '            Dim flag_KoTrung As Boolean = True

        '            For i As Integer = 0 To list_ContType.Count - 1
        '                If list_ContType.Item(i) = listCont.Item(j) Then
        '                    flag_KoTrung = False
        '                End If
        '            Next

        '            If flag_KoTrung Then
        '                list_ContType.Add(listCont.Item(j))
        '            End If
        '        Next
        '    Next


        '    Dim list_ContType As New ArrayList
        '    'Lay danh sach conttype da nhap trong cac HBL
        '    For Each HBL As tblHBL In listHBL
        '        Dim ID_HBL As Integer = HBL.ID_HBL
        '        Dim listCont = (From p In db.tblConts
        '                            Where p.ID_HBL = ID_HBL
        '                            Select p.ID_ContType).Distinct.ToList

        '        'kiem tra trung loai conttype

        '        For j As Integer = 0 To listCont.Count - 1
        '            Dim flag_KoTrung As Boolean = True

        '            For i As Integer = 0 To list_ContType.Count - 1
        '                If list_ContType.Item(i) = listCont.Item(j) Then
        '                    flag_KoTrung = False
        '                End If
        '            Next

        '            If flag_KoTrung Then
        '                list_ContType.Add(listCont.Item(j))
        '            End If
        '        Next
        '    Next

        '    'a. Insert Buying
        '    Dim listFee_Buying = From p In db.tblFees
        '        Where (p.ID_FeeType = 1 And p.IsBill = False And p.IsActive = True)
        '        Select p

        '    For Each fee In listFee_Buying
        '        For i As Integer = 0 To list_ContType.Count - 1
        '            Dim obj_FT As New tblFreightTariff
        '            obj_FT.ID_Voyage = obj_Voyage.ID_Voyage
        '            obj_FT.ID_Fee = fee.ID_Fee
        '            obj_FT.ID_ContType = list_ContType.Item(i)
        '            obj_FT.Price = 0
        '            obj_FT.ID_Currency = File.ID_Currency
        '            obj_FT.IsBuying = True

        '            db.tblFreightTariffs.InsertOnSubmit(obj_FT)
        '            db.SubmitChanges()
        '        Next
        '    Next
        '    'b. Insert Selling
        '    Dim listFee_Selling = From p In db.tblFees
        '        Where (p.ID_FeeType = 2 And p.IsBill = False And p.IsActive = True)
        '        Select p

        '    For Each fee In listFee_Selling
        '        For i As Integer = 0 To list_ContType.Count - 1
        '            Dim obj_FT As New tblFreightTariff
        '            obj_FT.ID_Voyage = obj_Voyage.ID_Voyage
        '            obj_FT.ID_Fee = fee.ID_Fee
        '            obj_FT.ID_ContType = list_ContType.Item(i)
        '            obj_FT.Price = 0
        '            obj_FT.ID_Currency = File.ID_Currency
        '            obj_FT.IsBuying = False

        '            db.tblFreightTariffs.InsertOnSubmit(obj_FT)
        '            db.SubmitChanges()
        '        Next
        '    Next
        'End If

        KhoiTaodtgFreightTariff(obj_Voyage.ID_Voyage)


        'Insert FeeBill not in Voyage Fee
        Dim list_VoyageFee = From p In db.tblVoyageFees
                             Where p.ID_Voyage = obj_Voyage.ID_Voyage
                             Select p

        Dim list_FeeBill = From p In db.tblFees
                           Where p.IsBill = True And p.IsActive = True
                           Select p

        For Each fee In list_FeeBill
            Dim flag_exist As Boolean = False
            For Each voyageFee In list_VoyageFee
                If fee.ID_Fee = voyageFee.ID_Fee Then
                    flag_exist = True
                    Exit For
                End If
            Next

            If Not flag_exist Then
                'Insert feebill
                Dim obj_VoyageFee As New tblVoyageFee
                obj_VoyageFee.ID_Voyage = obj_Voyage.ID_Voyage
                obj_VoyageFee.ID_Fee = fee.ID_Fee
                obj_VoyageFee.Price = 0
                obj_VoyageFee.ID_Currency = 2 'File.ID_Currency ' Tien mac dinh

                If fee.ID_FeeType = 1 Then
                    obj_VoyageFee.IsBuying = True
                ElseIf fee.ID_FeeType = 2 Then
                    obj_VoyageFee.IsBuying = False
                End If

                db.tblVoyageFees.InsertOnSubmit(obj_VoyageFee)
                db.SubmitChanges()
            End If
        Next

        ''Kiem tra da co VoyageFee chưa? Nếu chưa thì tạo
        'Dim c_VoyageFee = (From p In db.tblVoyageFees
        '                   Where p.ID_Voyage = obj_Voyage.ID_Voyage
        '                   Select p).Count

        'If c_VoyageFee = 0 Then

        '    '1. Insert Voyage Fee
        '    Dim feeBillBuying = From p In db.tblFees
        '                  Where p.IsBill = True And p.ID_FeeType = 1 And p.IsActive = True
        '                  Select p

        '    For Each fee As tblFee In feeBillBuying
        '        Dim obj_VoyageFeeBuying As New tblVoyageFee
        '        obj_VoyageFeeBuying.ID_Voyage = obj_Voyage.ID_Voyage
        '        obj_VoyageFeeBuying.ID_Fee = fee.ID_Fee
        '        obj_VoyageFeeBuying.Price = 0
        '        obj_VoyageFeeBuying.ID_Currency = 1 ' Tien VND
        '        obj_VoyageFeeBuying.IsBuying = True
        '        db.tblVoyageFees.InsertOnSubmit(obj_VoyageFeeBuying)
        '        db.SubmitChanges()
        '    Next

        '    Dim feeBillSelling = From p In db.tblFees
        '                  Where p.IsBill = True And p.ID_FeeType = 2 And p.IsActive = True
        '                  Select p

        '    For Each fee As tblFee In feeBillSelling
        '        Dim obj_VoyageFeeSelling As New tblVoyageFee
        '        obj_VoyageFeeSelling.ID_Voyage = obj_Voyage.ID_Voyage
        '        obj_VoyageFeeSelling.ID_Fee = fee.ID_Fee
        '        obj_VoyageFeeSelling.Price = 0
        '        obj_VoyageFeeSelling.ID_Currency = 1 ' Tien VND
        '        obj_VoyageFeeSelling.IsBuying = False
        '        db.tblVoyageFees.InsertOnSubmit(obj_VoyageFeeSelling)
        '        db.SubmitChanges()
        '    Next

        'End If
        '4. VoyageFee (Phí theo Bill)

        'Insert BuyingFee and SellingFee

        KhoiTaodtgVoyageFee(obj_Voyage.ID_Voyage)

    End Sub

    Public Sub KhoiTaodtgVoyageFee(ByVal ID_Voyage As Integer)
        '1. Buying
        Dim VoyageFeeBuying = From p In db.tblVoyageFees
                        Where p.ID_Voyage = ID_Voyage And p.IsBuying = True And p.tblFee.IsActive = True
                        Order By p.tblFee.Rank
                        Select New With {p.tblFee.FeeName, p.ID_Fee, p.ID_Voyage, p.Price, p.ID_Currency}


        dtgVoyageFeeBuying.DataSource = VoyageFeeBuying

        '2. Selling
        Dim VoyageFeeSelling = From p In db.tblVoyageFees
                        Where p.ID_Voyage = ID_Voyage And p.IsBuying = False And p.tblFee.IsActive = True
                        Order By p.tblFee.Rank
                        Select New With {p.tblFee.FeeName, p.ID_Fee, p.ID_Voyage, p.Price, p.ID_Currency}

        dtgVoyageFeeSelling.DataSource = VoyageFeeSelling
        FormatdtgVoyageFee()
    End Sub

    Public Sub FormatdtgVoyageFee()
        '1. Buying
        dtgVoyageFeeBuying.RowHeadersWidth = 20
        dtgVoyageFeeBuying.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgVoyageFeeBuying.RowTemplate.Height = 25
        dtgVoyageFeeBuying.EditMode = DataGridViewEditMode.EditOnEnter

        dtgVoyageFeeBuying.MultiSelect = False
        'dtgVoyageFee.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgVoyageFeeBuying.AllowUserToResizeColumns = False
        dtgVoyageFeeBuying.BackgroundColor = Color.White
        dtgVoyageFeeBuying.AllowUserToAddRows = False
        dtgVoyageFeeBuying.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgVoyageFeeBuying.Columns("ID_Fee").Visible = False
        dtgVoyageFeeBuying.Columns("ID_Voyage").Visible = False
        dtgVoyageFeeBuying.Columns("ID_Currency").Visible = False

        With dtgVoyageFeeBuying.Columns("FeeName")
            .HeaderText = "Fee"
            .Width = 70
            .ReadOnly = True
        End With

        With dtgVoyageFeeBuying.Columns("Price")
            .Width = 80
            .DefaultCellStyle.Format = "#,###.##"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        '2. Selling
        dtgVoyageFeeSelling.RowHeadersWidth = 20
        dtgVoyageFeeSelling.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgVoyageFeeSelling.RowTemplate.Height = 25
        dtgVoyageFeeSelling.EditMode = DataGridViewEditMode.EditOnEnter

        dtgVoyageFeeSelling.AllowUserToResizeColumns = False
        dtgVoyageFeeSelling.MultiSelect = False
        'dtgVoyageFee.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgVoyageFeeSelling.BackgroundColor = Color.White
        dtgVoyageFeeSelling.AllowUserToAddRows = False
        dtgVoyageFeeSelling.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgVoyageFeeSelling.Columns("ID_Fee").Visible = False
        dtgVoyageFeeSelling.Columns("ID_Voyage").Visible = False
        dtgVoyageFeeSelling.Columns("ID_Currency").Visible = False

        With dtgVoyageFeeSelling.Columns("FeeName")
            .HeaderText = "Fee"
            .Width = 70
            .ReadOnly = True
        End With
        With dtgVoyageFeeSelling.Columns("Price")
            .Width = 80
            .DefaultCellStyle.Format = "#,###.##"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With

        ''For i As Integer = 2 To dtBangCuocBuying.Columns.Count - 1
        ''    With dtgFreightTariff.Columns(i)
        ''        .DefaultCellStyle.Format = "#,###"
        ''        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        ''        .Width = 60
        ''    End With
        ''Next
    End Sub

    Private Sub AddComboBoxColumns()
        Dim comboboxColumn As DataGridViewComboBoxColumn
        comboboxColumn = CreateComboBoxColumn()
        SetAlternateChoicesUsingDataSource(comboboxColumn)
        comboboxColumn.HeaderText = "Currency"
        dtgVoyageFeeBuying.Columns.Add(comboboxColumn)


        Dim cbbCurrency As DataGridViewComboBoxColumn
        cbbCurrency = TaoColumscbb("Currency")
        dtgVoyageFeeSelling.Columns.Add(cbbCurrency)
    End Sub

    Private Function CreateComboBoxColumn() As DataGridViewComboBoxColumn
        Dim column As New DataGridViewComboBoxColumn()

        With column
            .DataPropertyName = "ID_Currency"
            .HeaderText = "Currency"
            .DropDownWidth = 90
            .Width = 90
            .MaxDropDownItems = 3
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
            .FlatStyle = FlatStyle.Flat
        End With
        Return column
    End Function

    Private Sub SetAlternateChoicesUsingDataSource(ByVal comboboxColumn As DataGridViewComboBoxColumn)
        'Dim ID_Currency_File As Integer
        'ID_Currency_File = (From p In db.tblFiles
        '                   Where p.ID_File = m_ID_File
        '                   Select p.ID_Currency).Single

        'Dim CurrencyList = From p In db.tblCurrencies
        '                   Where p.ID_Currency = 1 Or p.ID_Currency = ID_Currency_File
        '               Select p

        Dim CurrencyList = From p In db.tblCurrencies
                           Where p.IsActive = True
                           Select p

        With comboboxColumn
            .DataSource = CurrencyList
            .ValueMember = "ID_Currency"
            .DisplayMember = "Code"
        End With
    End Sub

    Public Function TaoColumscbb(ByVal colName As String) As DataGridViewComboBoxColumn
        'Dim ID_Currency_File As Integer
        'ID_Currency_File = (From p In db.tblFiles
        '                   Where p.ID_File = m_ID_File
        '                   Select p.ID_Currency).Single

        'Dim CurrencyList = From p In db.tblCurrencies
        '                   Where p.ID_Currency = 1 Or p.ID_Currency = ID_Currency_File
        '               Select p

        Dim CurrencyList = From p In db.tblCurrencies
                           Where p.IsActive = True
                           Select p

        Dim cbbCurrency As New DataGridViewComboBoxColumn

        With cbbCurrency
            .DataPropertyName = "ID_Currency"
            .HeaderText = "Currency"
            .DropDownWidth = 90
            .Width = 90
            .MaxDropDownItems = 3
            .FlatStyle = FlatStyle.Flat
        End With

        cbbCurrency.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
        cbbCurrency.Name = colName
        cbbCurrency.HeaderText = "Currency"
        cbbCurrency.DataSource = CurrencyList
        cbbCurrency.DisplayMember = "Code"
        cbbCurrency.ValueMember = "ID_Currency"
        Return cbbCurrency
    End Function

    Public Sub KhoiTaodtgFreightTariff(ByVal ID_Voyage As Integer)
        Try
            dtBangCuocBuying = GetBangCuoc(ID_Voyage, True)
            dtBangCuocSelling = GetBangCuoc(ID_Voyage, False)
            dtgFreightTariffBuying.DataSource = dtBangCuocBuying
            dtgFreightTariffSelling.DataSource = dtBangCuocSelling
            FormatdtgFreightTariff()
        Catch ex As Exception
            MsgBox("Khởi tạo bảng cước vận tải: " + ex.Message)
        End Try
    End Sub

    Public Sub FormatdtgFreightTariff()
        '1. Buying
        dtgFreightTariffBuying.RowHeadersWidth = 20
        dtgFreightTariffBuying.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgFreightTariffBuying.RowTemplate.Height = 25


        dtgFreightTariffBuying.MultiSelect = False
        'dtgFreightTariff.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgFreightTariffBuying.EditMode = DataGridViewEditMode.EditOnEnter
        dtgFreightTariffBuying.BackgroundColor = Color.White
        dtgFreightTariffBuying.AllowUserToAddRows = False
        dtgFreightTariffBuying.AllowUserToDeleteRows = False
        dtgFreightTariffBuying.AllowUserToResizeColumns = False
        dtgFreightTariffBuying.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgFreightTariffBuying.Columns("ID_ContType").Visible = False
        With dtgFreightTariffBuying.Columns("ContTypeName")
            .HeaderText = "Cont Type"
            .Width = 150
            .ReadOnly = True
        End With

        For i As Integer = 2 To dtBangCuocBuying.Columns.Count - 1
            With dtgFreightTariffBuying.Columns(i)
                .DefaultCellStyle.Format = "#,###.##"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Width = 80
            End With
        Next

        '1. Selling
        dtgFreightTariffSelling.RowHeadersWidth = 20
        dtgFreightTariffSelling.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgFreightTariffSelling.RowTemplate.Height = 25
        dtgFreightTariffSelling.AllowUserToResizeColumns = False

        dtgFreightTariffSelling.MultiSelect = False
        'dtgFreightTariffSelling.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgFreightTariffSelling.EditMode = DataGridViewEditMode.EditOnEnter

        dtgFreightTariffSelling.BackgroundColor = Color.White
        dtgFreightTariffSelling.AllowUserToAddRows = False
        dtgFreightTariffSelling.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgFreightTariffSelling.Columns("ID_ContType").Visible = False
        With dtgFreightTariffSelling.Columns("ContTypeName")
            .HeaderText = "Cont Type"
            .Width = 150
            .ReadOnly = True
        End With

        For i As Integer = 2 To dtBangCuocSelling.Columns.Count - 1
            With dtgFreightTariffSelling.Columns(i)
                .DefaultCellStyle.Format = "#,###.##"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Width = 80
            End With
        Next
    End Sub


    'Tao list combobox Currency with Name = ID_Fee 
    Dim listcbbCurrency As New ArrayList
    Dim listcbbCurrencySelling As New ArrayList

    Public Sub KhoiTaocbbCurrency()
        'Create cbbCurrency for each Fee
        Dim listFee = From p In db.tblFees
                Where p.ID_FeeType = 1 And p.IsBill = False And p.IsActive = True
                Select p
                Order By p.Rank
        Dim i As Integer = 0

        For Each fee As tblFee In listFee
            Dim cbbCurrency As New DevComponents.DotNetBar.Controls.ComboBoxEx

            ExpnlBuying.Controls.Add(cbbCurrency)
            cbbCurrency.Name = fee.ID_Fee.ToString

            'Dim ID_Currency_File As Integer
            'ID_Currency_File = (From p In db.tblFiles
            '                   Where p.ID_File = m_ID_File
            '                   Select p.ID_Currency).Single

            'Dim CurrencyList = From p In db.tblCurrencies
            '                   Where p.ID_Currency = 1 Or p.ID_Currency = ID_Currency_File
            '               Select p

            Dim CurrencyList = From p In db.tblCurrencies
                               Where p.IsActive = True
                               Select p

            cbbCurrency.DataSource = CurrencyList
            cbbCurrency.DisplayMember = "Code"
            cbbCurrency.ValueMember = "ID_Currency"
            cbbCurrency.Width = 80
            cbbCurrency.DrawMode = DrawMode.OwnerDrawFixed
            cbbCurrency.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled

            cbbCurrency.Location = New Point(150 + 20 + i * 80, 3)
            i += 1
            cbbCurrency.SelectedValue = 2 ' Chọn tiền USD
            listcbbCurrency.Add(cbbCurrency)
        Next
    End Sub

    Public Sub KhoiTaocbbCurrencySelling()
        'Create cbbCurrency for each Fee
        'Dim ID_Currency_File As Integer
        'ID_Currency_File = (From p In db.tblFiles
        '                   Where p.ID_File = m_ID_File
        '                   Select p.ID_Currency).Single

        Dim listFee = From p In db.tblFees
                Where p.ID_FeeType = 2 And p.IsBill = False And p.IsActive = True
                Select p
                Order By p.Rank

        Dim i As Integer = 0

        For Each fee As tblFee In listFee
            Dim cbbCurrency As New DevComponents.DotNetBar.Controls.ComboBoxEx

            ExpnlSelling.Controls.Add(cbbCurrency)
            cbbCurrency.Name = fee.ID_Fee.ToString

            'Dim CurrencyList = From p In db.tblCurrencies
            '                   Where p.ID_Currency = 1 Or p.ID_Currency = ID_Currency_File
            '               Select p
            Dim CurrencyList = From p In db.tblCurrencies
                               Where p.IsActive = True
                               Select p

            cbbCurrency.DataSource = CurrencyList
            cbbCurrency.DisplayMember = "Code"
            cbbCurrency.ValueMember = "ID_Currency"
            cbbCurrency.Width = 80
            cbbCurrency.DrawMode = DrawMode.OwnerDrawFixed
            cbbCurrency.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled

            cbbCurrency.Location = New Point(150 + 20 + i * 80, 3)
            i += 1
            cbbCurrency.SelectedValue = 2 ' Chọn tiền USD
            listcbbCurrencySelling.Add(cbbCurrency)
        Next
    End Sub

    Public Function GetBangCuoc(ByVal ID_Voyage As Integer, ByVal IsBuying As Boolean) As DataTable

        Dim listFee As IQueryable(Of tblFee)
        If IsBuying Then
            listFee = From p In db.tblFees
                Where (p.ID_FeeType = 1 And p.IsBill = False And p.IsActive = True)
                Select p
                Order By p.Rank
        Else
            listFee = From p In db.tblFees
                Where (p.ID_FeeType = 2 And p.IsBill = False And p.IsActive = True)
                Select p
                Order By p.Rank
        End If

        Dim listContType = (From p In db.tblFreightTariffs
                Where p.ID_Voyage = ID_Voyage And p.IsBuying = IsBuying
                Select ID_ContType = p.ID_ContType).Distinct.ToList

        Dim contType = From p In db.tblContTypes
                       Where listContType.Contains(p.ID_ContType)
                       Select p

        Dim dt As New DataTable
        'Row la loai container
        'Col la cac loai phi cua hang tau theo tung loai cont

        Dim column As DataColumn
        column = New DataColumn()
        column.DataType = System.Type.GetType("System.Int32")
        column.ColumnName = "ID_ContType"

        ' Add the Column to the DataColumnCollection.
        dt.Columns.Add(column)

        ' Create second column.
        column = New DataColumn()
        column.DataType = System.Type.GetType("System.String")
        column.ColumnName = "ContTypeName"

        ' Add the column to the table.
        dt.Columns.Add(column)

        For Each f As tblFee In listFee
            Dim feeName As String
            Dim ID_Fee As Integer

            feeName = f.FeeName
            ID_Fee = f.ID_Fee

            column = New DataColumn()
            column.DataType = System.Type.GetType("System.Double")
            column.ColumnName = feeName
            column.Caption = ID_Fee
            dt.Columns.Add(column)
        Next

        If contType.Count > 0 Then
            For Each ct As tblContType In contType
                Dim row As DataRow
                row = dt.NewRow
                row(0) = ct.ID_ContType
                row(1) = ct.ContTypeName

                For i As Integer = 2 To dt.Columns.Count - 1
                    row(i) = GetPrice(ID_Voyage, ct.ID_ContType, CInt(dt.Columns(i).Caption), IsBuying)
                Next
                dt.Rows.Add(row)
            Next

            'Khoi tao gia tri cbbCurrency
            For i As Integer = 0 To listcbbCurrency.Count - 1
                Dim cbb As DevComponents.DotNetBar.Controls.ComboBoxEx
                cbb = listcbbCurrency.Item(i)
                Dim ID_Fee As Integer = CInt(cbb.Name)

                Try
                    Dim Currency = (From p In db.tblFreightTariffs
                                  Where p.ID_Voyage = ID_Voyage And p.ID_Fee = ID_Fee
                                  Select p.ID_Currency).Distinct.Single

                    cbb.SelectedValue = Currency
                Catch ex As Exception
                    cbb.SelectedIndex = 0
                End Try

            Next

            'Khoi tao gia tri cbbCurrencySelling
            For i As Integer = 0 To listcbbCurrencySelling.Count - 1
                Dim cbb As DevComponents.DotNetBar.Controls.ComboBoxEx
                cbb = listcbbCurrencySelling.Item(i)
                Dim ID_Fee As Integer = CInt(cbb.Name)

                Try
                    Dim Currency = (From p In db.tblFreightTariffs
                                  Where p.ID_Voyage = ID_Voyage And p.ID_Fee = ID_Fee
                                  Select p.ID_Currency).Distinct.Single

                    cbb.SelectedValue = Currency
                Catch ex As Exception
                    cbb.SelectedIndex = 0
                End Try

            Next
        End If
        Return dt
    End Function

    Public Function GetPrice(ByVal ID_Voyage As Integer, ByVal ID_ContType As Integer, ByVal ID_Fee As Integer, ByVal IsBuying As Boolean) As Double
        Dim kq As Double = 0
        Try
            Dim ft = (From p In db.tblFreightTariffs
                    Where p.ID_Voyage = ID_Voyage And p.ID_ContType = ID_ContType And p.ID_Fee = ID_Fee And p.IsBuying = IsBuying
                    Select p).Single
            kq = ft.Price
        Catch ex As Exception

        End Try

        Return kq
    End Function

#Region "Them, Xoa, Sua Voyage"
    'Dim fV_Them As Boolean = False
    'Dim fV_Sua As Boolean = False

    'Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    '    fV_Them = True
    '    ClearVoyage()
    '    KhoiTao_GiaoDien(False)
    'End Sub

    'Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    '    fV_Sua = True
    '    KhoiTao_GiaoDien(False)
    'End Sub

    'Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
    '    'sy
    '    'chua kiem tra rang buoc du lieu

    '    Dim VoyageNo As String = ""
    '    VoyageNo = obj_Voyage.VoyageNo

    '    If MsgBox("Bạn muốn xóa chuyến tàu số: " + VoyageNo, MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes Then
    '        Dim ID_Voyage As Integer
    '        ID_Voyage = obj_Voyage.ID_Voyage

    '        '1. Del Freight Tariff
    '        Dim ft = From p In db.tblFreightTariffs
    '                 Where p.ID_Voyage = ID_Voyage
    '                 Select p

    '        db.tblFreightTariffs.DeleteAllOnSubmit(ft)

    '        '2. Del Voyage Fee
    '        Dim voyageFee = From p In db.tblVoyageFees
    '                        Where p.ID_Voyage = ID_Voyage
    '                        Select p

    '        db.tblVoyageFees.DeleteAllOnSubmit(voyageFee)

    '        '3. Del Voyage
    '        Dim voyage = From p In db.tblVoyages
    '                     Where p.ID_Voyage = ID_Voyage
    '                     Select p

    '        db.tblVoyages.DeleteAllOnSubmit(voyage)

    '        db.SubmitChanges()
    '        'Refesh
    '        KhoiTaoMBL_Voyage_FreightTariff(m_ID_File)
    '        'KhoiTaodtgFreightTariff(ID_Voyage)
    '        'KhoiTaodtgVoyageFee(ID_Voyage)
    '    End If
    'End Sub

    'Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    '    'Kiem tra du lieu
    '    If cbbPOD.SelectedIndex = -1 Or cbbPOL.SelectedIndex = -1 Then
    '        MsgBox("Bạn chưa chọn Cảng đi hoặc Cảng đến")
    '        cbbPOL.Focus()
    '        Exit Sub
    '    End If

    '    If cbbLine.SelectedIndex = -1 Then
    '        MsgBox("Bạn chưa chọn Hãng tàu")
    '        cbbLine.Focus()
    '        Exit Sub
    '    End If

    '    If cbbVessel.SelectedIndex = -1 Then
    '        MsgBox("Bạn chưa chọn Tên tàu")
    '        cbbVessel.Focus()
    '        Exit Sub
    '    End If

    '    'ID_Currency = cbbCurrency.SelectedValue
    '    'If ID_Currency = 0 Then
    '    '    MsgBox("Bạn chưa chọn ngoại tệ, tỷ giá cho bảng cước vận chuyển")
    '    '    cbbCurrency.Focus()
    '    '    Exit Sub
    '    'End If


    '    If fV_Them Then
    '        obj_Voyage = New tblVoyage
    '        obj_Voyage.ID_Line = cbbLine.SelectedValue
    '        obj_Voyage.ID_Vessel = cbbVessel.SelectedValue
    '        obj_Voyage.ID_POD = cbbPOD.SelectedValue
    '        obj_Voyage.ID_POL = cbbPOL.SelectedValue
    '        obj_Voyage.VoyageNo = txtVoyageNo.Text

    '        If dtpETATime.Text <> "" Then
    '            obj_Voyage.ETA = dtpETA.Value.ToShortDateString + " " + dtpETATime.Value.ToShortTimeString
    '        Else
    '            obj_Voyage.ETA = dtpETA.Value.ToShortDateString + " 00:00:00 AM"
    '        End If

    '        If dtpETDTime.Text <> "" Then
    '            obj_Voyage.ETD = dtpETD.Value.ToShortDateString + " " + dtpETDTime.Value.ToShortTimeString
    '        Else
    '            obj_Voyage.ETD = dtpETD.Value.ToShortDateString + " 00:00:00 AM"
    '        End If

    '        m_Voyage.Insert(obj_Voyage)

    '        'Update tblMBL Set ID_Voyage
    '        obj_MBL.ID_Voyage = obj_Voyage.ID_Voyage
    '        m_MBL.Update(obj_MBL)

    '        KhoiTaodtgFreightTariff(obj_Voyage.ID_Voyage)
    '        KhoiTaodtgVoyageFee(obj_Voyage.ID_Voyage)
    '        AddComboBoxColumns()
    '    ElseIf fV_Sua Then

    '        obj_Voyage.ID_Vessel = cbbVessel.SelectedValue
    '        obj_Voyage.ID_Line = cbbLine.SelectedValue
    '        obj_Voyage.ID_POD = cbbPOD.SelectedValue
    '        obj_Voyage.ID_POL = cbbPOL.SelectedValue
    '        obj_Voyage.VoyageNo = txtVoyageNo.Text

    '        If dtpETATime.Text <> "" Then
    '            obj_Voyage.ETA = dtpETA.Value.ToShortDateString + " " + dtpETATime.Value.ToShortTimeString
    '        Else
    '            obj_Voyage.ETA = dtpETA.Value.ToShortDateString + " 00:00:00 AM"
    '        End If

    '        If dtpETDTime.Text <> "" Then
    '            obj_Voyage.ETD = dtpETD.Value.ToShortDateString + " " + dtpETDTime.Value.ToShortTimeString
    '        Else
    '            obj_Voyage.ETD = dtpETD.Value.ToShortDateString + " 00:00:00 AM"
    '        End If

    '        m_Voyage.Update(obj_Voyage)
    '    End If

    '    fV_Them = False
    '    fV_Sua = False
    '    KhoiTao_GiaoDien(True)
    'End Sub

    'Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    '    fV_Them = False
    '    fV_Sua = False
    '    'Refesh
    '    KhoiTao_GiaoDien(True)
    'End Sub

    'Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
    '    ''Copy Voyage and FreightTariff
    '    'Dim voyage As New tblVoyage
    '    'voyage.ID_Vessel = dtgVoyage.CurrentRow.Cells("ID_Vessel").Value
    '    'voyage.ETA = dtgVoyage.CurrentRow.Cells("ETA").Value
    '    'voyage.ETD = dtgVoyage.CurrentRow.Cells("ETD").Value
    '    'voyage.ID_POD = dtgVoyage.CurrentRow.Cells("ID_POD").Value
    '    'voyage.ID_POL = dtgVoyage.CurrentRow.Cells("ID_POL").Value
    '    'voyage.VoyageNo = "" 'dtgVoyage.CurrentRow.Cells("VoyageNo").Value

    '    'db.tblVoyages.InsertOnSubmit(voyage)
    '    'db.SubmitChanges()

    '    'Dim ID_Voyage As Integer
    '    'ID_Voyage = voyage.ID_Voyage

    '    'Dim ID_Voyage_Copy As Integer = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value

    '    'Dim ft = From p In db.tblFreightTariffs
    '    '          Where p.ID_Voyage = ID_Voyage_Copy
    '    '          Select p

    '    'For Each freightTariff In ft
    '    '    Dim new_ft As New tblFreightTariff
    '    '    new_ft.ID_Voyage = ID_Voyage
    '    '    new_ft.ID_Fee = freightTariff.ID_Fee
    '    '    new_ft.ID_ContType = freightTariff.ID_ContType
    '    '    new_ft.Price = freightTariff.Price
    '    '    db.tblFreightTariffs.InsertOnSubmit(new_ft)
    '    'Next
    '    'db.SubmitChanges()


    '    'Try
    '    '    dtgVoyage.Rows(0).Selected = True
    '    '    dtgVoyage.SelectedCells(2).Selected = True
    '    '    btnEdit_Click(Nothing, Nothing)
    '    'Catch ex As Exception

    '    'End Try

    'End Sub

    'Public Sub ClearVoyage()
    '    txtVoyageNo.Text = ""
    '    cbbLine.SelectedIndex = -1
    '    cbbVessel.SelectedIndex = -1

    '    cbbPOL.SelectedIndex = -1
    '    cbbPOD.SelectedIndex = -1
    '    dtpETA.Value = Now.Date
    '    dtpETD.Value = Now.Date
    '    dtgFreightTariff.DataSource = Nothing
    '    dtgVoyageFee.DataSource = Nothing
    'End Sub

    'Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
    '    'set ReadOnly,visible,enable

    '    btnAdd.Enabled = flag
    '    btnEdit.Enabled = flag
    '    btnDel.Enabled = flag
    '    btnUpdate.Enabled = Not flag
    '    btnCancel.Enabled = Not flag
    '    btnCopy.Enabled = flag


    '    txtVoyageNo.ReadOnly = flag
    '    dtpETA.Enabled = Not flag
    '    dtpETD.Enabled = Not flag
    '    dtpETATime.Enabled = Not flag
    '    dtpETDTime.Enabled = Not flag

    '    cbbPOD.Enabled = Not flag
    '    cbbPOL.Enabled = Not flag
    '    cbbLine.Enabled = Not flag
    '    cbbVessel.Enabled = Not flag

    '    'set backcolor
    '    If (flag) Then
    '        txtVoyageNo.BackColor = Color.Azure
    '    Else
    '        txtVoyageNo.BackColor = Color.White
    '    End If
    'End Sub
#End Region

#Region "btn Them, Xoa, Sua Freight Tariff and Voyage Fee"
    Private Sub btnGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGhi.Click
        Dim ID_Voyage As Integer
        ID_Voyage = obj_Voyage.ID_Voyage


        '1. Xoa het bang cuoc 
        Dim ft = From p In db.tblFreightTariffs
                 Where p.ID_Voyage = ID_Voyage
                 Select p
        db.tblFreightTariffs.DeleteAllOnSubmit(ft)
        db.SubmitChanges()

        'A . BUYING
        '2. Them moi bang cuoc
        For Each row As DataRow In dtBangCuocBuying.Rows
            Dim ID_ContType As Integer
            ID_ContType = row.Item("ID_ContType")
            For i As Integer = 2 To dtBangCuocBuying.Columns.Count - 1
                Dim ID_Fee As Integer
                ID_Fee = dtBangCuocBuying.Columns(i).Caption
                Dim price As Double = 0
                Try
                    price = row.Item(i)
                Catch ex As Exception

                End Try
                'MsgBox("Bo 3: ID_Voyage=" & ID_Voyage & " ID_ContType=" & ID_ContType & " ID_Fee=" & ID_Fee)
                Dim f As New tblFreightTariff
                f.ID_Voyage = ID_Voyage
                f.ID_ContType = ID_ContType
                f.ID_Fee = ID_Fee
                f.Price = price
                f.IsBuying = True

                Dim cbb As DevComponents.DotNetBar.Controls.ComboBoxEx
                cbb = listcbbCurrency.Item(i - 2)
                f.ID_Currency = cbb.SelectedValue
                db.tblFreightTariffs.InsertOnSubmit(f)
                db.SubmitChanges()
            Next
        Next

        '3. Update Voyage Fee
        For i As Integer = 0 To dtgVoyageFeeBuying.RowCount - 1
            Dim obj_VoyageFee As New tblVoyageFee
            obj_VoyageFee.ID_Voyage = ID_Voyage
            obj_VoyageFee.ID_Fee = dtgVoyageFeeBuying.Rows(i).Cells("ID_Fee").Value
            obj_VoyageFee.Price = dtgVoyageFeeBuying.Rows(i).Cells("Price").Value
            obj_VoyageFee.ID_Currency = dtgVoyageFeeBuying.Rows(i).Cells("ID_Currency").Value
            obj_VoyageFee.IsBuying = True
            Dim m_voyagefee As New CVoyageFee
            m_voyagefee.Update(obj_VoyageFee)
        Next

        'B. SELLING
        '2. 
        For Each row As DataRow In dtBangCuocSelling.Rows
            Dim ID_ContType As Integer
            ID_ContType = row.Item("ID_ContType")
            For i As Integer = 2 To dtBangCuocSelling.Columns.Count - 1
                Dim ID_Fee As Integer
                ID_Fee = dtBangCuocSelling.Columns(i).Caption
                Dim price As Double = 0
                Try
                    price = row.Item(i)
                Catch ex As Exception

                End Try
                'MsgBox("Bo 3: ID_Voyage=" & ID_Voyage & " ID_ContType=" & ID_ContType & " ID_Fee=" & ID_Fee)
                Dim f As New tblFreightTariff
                f.ID_Voyage = ID_Voyage
                f.ID_ContType = ID_ContType
                f.ID_Fee = ID_Fee
                f.Price = price
                f.IsBuying = False

                Dim cbb As DevComponents.DotNetBar.Controls.ComboBoxEx
                cbb = listcbbCurrencySelling.Item(i - 2)
                f.ID_Currency = cbb.SelectedValue
                db.tblFreightTariffs.InsertOnSubmit(f)
                db.SubmitChanges()
            Next
        Next

        '3. Update Voyage Fee
        For i As Integer = 0 To dtgVoyageFeeSelling.RowCount - 1
            Dim obj_VoyageFee As New tblVoyageFee
            obj_VoyageFee.ID_Voyage = ID_Voyage
            obj_VoyageFee.ID_Fee = dtgVoyageFeeSelling.Rows(i).Cells("ID_Fee").Value
            obj_VoyageFee.Price = dtgVoyageFeeSelling.Rows(i).Cells("Price").Value
            obj_VoyageFee.ID_Currency = dtgVoyageFeeSelling.Rows(i).Cells("ID_Currency").Value
            obj_VoyageFee.IsBuying = False
            Dim m_voyagefee As New CVoyageFee
            m_voyagefee.Update(obj_VoyageFee)
        Next

        KhoiTaoGiaoDien(True)
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If MsgBox("Bạn muốn xóa bảng cước vận tải?", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes Then
            Dim ID_Voyage As Integer
            ID_Voyage = obj_Voyage.ID_Voyage

            '1. Del Freight Tariff
            Dim ft = From p In db.tblFreightTariffs
                     Where p.ID_Voyage = ID_Voyage
                     Select p
            db.tblFreightTariffs.DeleteAllOnSubmit(ft)

            '2. Del Voyage Fee
            Dim voyageFee = From p In db.tblVoyageFees
                            Where p.ID_Voyage = ID_Voyage
                            Select p

            db.tblVoyageFees.DeleteAllOnSubmit(voyageFee)

            db.SubmitChanges()

            'Refesh
            KhoiTaodtgFreightTariff(ID_Voyage)
            KhoiTaodtgVoyageFee(ID_Voyage)
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        KhoiTaoGiaoDien(False)
        dtgVoyageFeeBuying.Focus()
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        KhoiTaoMBL_Voyage_FreightTariff(m_ID_File)
        KhoiTaoGiaoDien(True)
    End Sub

    Private Sub lvContType_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvContType.DoubleClick
        If btnSua.Enabled Then
            Exit Sub
        End If

        Dim contTypeName As String
        Dim ID_ContType As Integer
        contTypeName = lvContType.SelectedItems(0).Text
        ID_ContType = lvContType.SelectedItems(0).Name

        'Kiểm tra tồn tại row
        For i As Integer = 0 To dtBangCuocBuying.Rows.Count - 1
            If dtBangCuocBuying.Rows(i).Item("ID_ContType") = ID_ContType Then
                MsgBox("Đã có loại cont " + contTypeName + " trong bảng cước.")
                Exit Sub
            End If
        Next

        Dim rowBuying As DataRow
        rowBuying = dtBangCuocBuying.NewRow
        rowBuying(0) = ID_ContType
        rowBuying(1) = contTypeName

        For i As Integer = 2 To dtBangCuocBuying.Columns.Count - 1
            rowBuying(i) = 0
        Next
        dtBangCuocBuying.Rows.Add(rowBuying)

        Dim rowSelling As DataRow
        rowSelling = dtBangCuocSelling.NewRow
        rowSelling(0) = ID_ContType
        rowSelling(1) = contTypeName

        For i As Integer = 2 To dtBangCuocSelling.Columns.Count - 1
            rowSelling(i) = 0
        Next
        dtBangCuocSelling.Rows.Add(rowSelling)

    End Sub

    'Private Sub dtgFreightTariff_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs)
    '    If (MsgBox("Bạn muốn xóa loại cont này?", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
    '        Dim ID_Voyage As Integer
    '        ID_Voyage = obj_Voyage.ID_Voyage

    '        Dim ID_ContType As Integer
    '        ID_ContType = dtgFreightTariffBuying.CurrentRow.Cells("ID_ContType").Value

    '        Dim ft = From p In db.tblFreightTariffs
    '                 Where p.ID_Voyage = ID_Voyage And p.ID_ContType = ID_ContType
    '                 Select p
    '        db.tblFreightTariffs.DeleteAllOnSubmit(ft)
    '        db.SubmitChanges()

    '        'KhoiTaodtgFreightTariff(ID_Voyage)
    '    Else
    '        e.Cancel = True
    '    End If
    'End Sub

    Public Sub KhoiTaoGiaoDien(ByVal flag As Boolean)
        dtgFreightTariffBuying.ReadOnly = flag
        dtgFreightTariffSelling.ReadOnly = flag
        dtgVoyageFeeBuying.ReadOnly = flag
        dtgVoyageFeeSelling.ReadOnly = flag

        For i As Integer = 0 To listcbbCurrency.Count - 1
            CType(listcbbCurrency.Item(i), ComboBox).Enabled = Not flag
        Next

        For i As Integer = 0 To listcbbCurrencySelling.Count - 1
            CType(listcbbCurrencySelling.Item(i), ComboBox).Enabled = Not flag
        Next

        Try
            dtgFreightTariffBuying.Columns("ContTypeName").ReadOnly = True
            dtgFreightTariffSelling.Columns("ContTypeName").ReadOnly = True
            dtgVoyageFeeBuying.Columns("FeeName").ReadOnly = True
            dtgVoyageFeeSelling.Columns("FeeName").ReadOnly = True
        Catch ex As Exception

        End Try
        

        btnGhi.Enabled = Not flag
        btnHuy.Enabled = Not flag
        btnSua.Enabled = flag
        btnXoa.Enabled = flag
    End Sub
#End Region

    Private Sub dtgFreightTariffBuying_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dtgFreightTariffBuying.DataError
        MsgBox("Dữ liệu phải là số")
    End Sub

    Private Sub dtgFreightTariffSelling_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dtgFreightTariffSelling.DataError
        MsgBox("Dữ liệu phải là số")
    End Sub

    Private Sub dtgVoyageFeeBuying_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dtgVoyageFeeBuying.DataError
        MsgBox("Dữ liệu phải là số")
    End Sub

    Private Sub dtgVoyageFeeSelling_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dtgVoyageFeeSelling.DataError
        MsgBox("Dữ liệu phải là số")
    End Sub
End Class

